package com.ctshk.app.user.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.user.dto.UserCollectionListDTO;
import com.ctshk.rpc.user.dto.UserFootPointGroupDTO;
import com.ctshk.rpc.user.dto.UserFootPointListDTO;
import com.ctshk.rpc.user.req.*;
import com.ctshk.rpc.user.service.IUserFootPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 *  我的足迹前端控制器
 * </p>
 *
 * @author 李新科
 * @date 2021/5/18 14:05
 */
@Api(value="我的足迹", tags="我的足迹")
@RestController
@RequestMapping("/footPrint")
public class FootPrintController extends AppBaseController {

    @DubboReference
    private IUserFootPointService userFootPointService;

    @ApiOperation(value="足迹列表", notes="足迹列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "足迹列表", operDesc = "足迹列表")
    @PostMapping("/list")
    public Result<PageResponse<UserFootPointListDTO>> queryPage(@RequestBody @Valid UserFootPointPageReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userFootPointService.queryPage(req);
    }

    @ApiOperation(value="足迹列表日期分组", notes="足迹列表日期分组")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "足迹列表日期分组", operDesc = "足迹列表日期分组")
    @PostMapping("/listGroupByDate")
    public Result<PageResponse<UserFootPointGroupDTO>> listGroupByDate(@RequestBody @Valid UserFootPointPageReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userFootPointService.listGroupByDate(req);
    }

    @ApiOperation(value="删除足迹", notes="删除足迹")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "删除足迹", operDesc = "删除足迹")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable @NotNull(message = "id不能为空")
                         @ApiParam(name="id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userFootPointService.delete(id,currentUser.getId());
    }

    @ApiOperation(value="清空足迹", notes="清空足迹")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "清空足迹", operDesc = "清空足迹")
    @PostMapping("/deleteAll")
    public Result deleteAll() throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userFootPointService.deleteAll(currentUser.getId());
    }

    @ApiOperation(value="测试新增足迹", notes="测试新增足迹")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.ADD, operTitle = "测试新增足迹", operDesc = "测试新增足迹")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid UserFootPointReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        CompletableFuture<Result> add = userFootPointService.add(req);
        return add.get();
    }
}
